<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

class KomaValidatorManager extends KomaSingletonObject
{
	private $_id, $_messages = array(), $_rules = array();
	
	function initializeValidator($id, $validators = array())
	{
		KomaScriptManager::getInstance()->addSource(KOMA_JQUERY_PATH);
		KomaScriptManager::getInstance()->addSource(KOMA_ROOT_PATH.'/resources/scripts/jquery.validate.min.js');
                KomaScriptManager::getInstance()->addSource(KOMA_ROOT_PATH.'/resources/scripts/koma.validation.js');
                
                $rules = array();
                $messages = array();

                if(count($validators) > 0)
                {
                        foreach($validators as $validator)
                        {
                                if($validator instanceof KomaValidator)
                                {
                                        $rules[''.$validator] = $validator->Param;
                                        $messages[''.$validator] = $validator->Message; 
                                }
                                
                        }
                        
                        if(count($rules) > 0)
                        {
                                $this->_rules[$id] = $rules;
                                $this->_messages[$id] = $messages;  
                        }
                        
                }
	}
	
	function addValidate($id)
	{
		KomaScriptManager::getInstance()->addScript('$(document).ready(function() { $("#'.$id.'").validate('.KomaValidatorManager::getInstance()->getValidator().'); });');
	}
	
	function getValidator()
	{
		return json_encode(array(rules => $this->_rules, 'messages' => $this->_messages));
	}
	
	static function getInstance()
	{
		return self::createInstance(__CLASS__);
	}
}

?>